package com.wgts.wgts.mvp.view.activity.wgtsteam;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerWgtsFansListActivityComponent;
import com.wgts.wgts.dagger.module.WgtsFansListActivityModule;
import com.wgts.wgts.mvp.presenter.IWgtsFansListActivityPresenter;
import com.wgts.wgts.mvp.view.IWgtsFansListActivityView;
import com.wgts.wgts.netmodle.resp.WgtsFansDetail;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WgtsFansListActivity extends BaseActivity implements IWgtsFansListActivityView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;

    @Inject
    IWgtsFansListActivityPresenter presenter;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private String title;

    private String relationLevel;

    private String userRole;

    public static final String TITLE = "TITLE";

    public static final String RELATION_LEVEL = "RELATIONLEVEL";

    public static final String USER_ROLE = "USERROLE";

    private WgtsFansAdapter adapter;

    private List<WgtsFansDetail> wgtsFansDetails;


    @Override
    public void init() {
        title = getIntent().getStringExtra(TITLE);
        relationLevel = getIntent().getStringExtra(RELATION_LEVEL);
        userRole = getIntent().getStringExtra(USER_ROLE);
        DaggerWgtsFansListActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .wgtsFansListActivityModule(new WgtsFansListActivityModule(this))
                .build()
                .inject(this);

        wgtsFansDetails = new ArrayList<>();
        adapter = new WgtsFansAdapter(this, wgtsFansDetails);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wgts_fans_list_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        presenter.initFansdetail(relationLevel, userRole);
        smartRefreshLayout.setEnableLoadmore(true);
        smartRefreshLayout.setEnableRefresh(true);

        includeTitle.setText(title);
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.moreFansDetal();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.initFansdetail(relationLevel, userRole);
            }
        });

        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(adapter);
    }

    @OnClick(R.id.back_btn_ly)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void onError(int status, String msg) {
        netErrorLy.setVisibility(View.VISIBLE);
    }


    @Override
    public void initFansDetail(List<WgtsFansDetail> fansDetails, boolean noMore) {
        netErrorLy.setVisibility(View.GONE);

        if(fansDetails.size() == 0){
            noneDataLy.setVisibility(View.VISIBLE);
        }else{
            noneDataLy.setVisibility(View.GONE);
        }

        wgtsFansDetails.clear();
        wgtsFansDetails.addAll(fansDetails);
        adapter.setNoMore(noMore);
        smartRefreshLayout.finishRefresh();
        adapter.notifyDataSetChanged();
        smartRefreshLayout.setEnableLoadmore(!noMore);
    }

    @Override
    public void moreFansDetail(List<WgtsFansDetail> fansDetails, boolean noMore) {
        netErrorLy.setVisibility(View.GONE);

        if(fansDetails.size() == 0){
            noneDataLy.setVisibility(View.VISIBLE);
        }else{
            noneDataLy.setVisibility(View.GONE);
        }

        wgtsFansDetails.addAll(fansDetails);
        adapter.setNoMore(noMore);
        smartRefreshLayout.finishLoadmore();
        adapter.notifyDataSetChanged();
        smartRefreshLayout.setEnableLoadmore(!noMore);
    }
}
